﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class UITouchObject : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler,IPointerClickHandler
{

    public Action<Vector2> onPointerDownBegin;//点下瞬间
    public Action<Vector2> onPointerDown;//pressed
    public Action<Vector2> onDrag;
    public Action<Vector2> onPointerUp;
    public Action<Vector2> onPointerClick;

   // bool isPointerDown = true;
    Vector2 m_PointerDownPosition;


    void Start()
    {

    }


    public void AddListener(Action<Vector2> onPointerDownBegin, Action<Vector2> onPointerDown, Action<Vector2> onDrag, Action<Vector2> onPointerUp)
    {
        this.onPointerDownBegin = onPointerDownBegin;
        this.onPointerDown = onPointerDown;
       // m_OnPointerClick = onClick;
        this.onDrag = onDrag;
        this.onPointerUp = onPointerUp;
    }

    const int NoPointerId = -100;
    int m_PointerId = NoPointerId;
    Vector2 lastMousePos;
    public void OnPointerDown(PointerEventData eventData)
    {
        if (m_PointerId == NoPointerId)
        {
            m_PointerId = eventData.pointerId;
            
            if (onPointerDownBegin != null)
                onPointerDownBegin(eventData.position);

            m_PointerDownPosition = eventData.position;
           // this.isPointerDown = true;
        }
    }


    public void OnDrag(PointerEventData eventData)
    {
      //  Debug.Log("OnDrag  eventData:" + eventData.position+"   name"+this.name);
        if (m_PointerId == eventData.pointerId)
        {
            if (onDrag != null)
                onDrag(eventData.position);
            
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (onPointerClick != null)
            onPointerClick(eventData.position);

    }

    public void OnPointerUp(PointerEventData eventData)
    {
        //GameDebug.LogError("OnPointerUp:" + eventData.pointerId);
        if (m_PointerId == eventData.pointerId)
        {
            m_PointerId = NoPointerId;

            if (onPointerUp != null)
                onPointerUp(eventData.position);
        }
        //isPointerDown = false;
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            if (this.onPointerDown != null)
                    onPointerDown(this.m_PointerDownPosition);
            
        }
        else
        {
            m_PointerId = NoPointerId;
        }
    }


}
